package com.service.admin.upload.controller;

import com.service.admin.base.service.ConfigService;
import com.service.admin.qiniu.service.FileService;
import com.service.admin.support.ProjectProperty;
import com.service.admin.upload.enums.FileEnum;
import com.service.core.exception.CommonException;
import com.service.core.rest.BaseController;
import com.service.core.rest.response.OneResponse;
import com.service.dao.constants.ConfigConstants;
import com.service.dao.model.SysConfig;
import com.service.utility.common.RandomUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;


@Slf4j
@RestController
@RequestMapping("file")
public class FileUploadController extends BaseController {

    @Autowired
    private ProjectProperty projectProperty;

    @Autowired
    private ConfigService configService;

    @Autowired
    private FileService fileService;

    /**
     * 实现文件上传
     */
    @RequestMapping("image-upload")
    public OneResponse<String> fileUpload(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            throw new CommonException("没有要上传的文件", "no_file_upload");
        }
        if (!StringUtils.equals(file.getContentType(), FileEnum.image.getType())) {
            throw new CommonException("文件格式错误,请重新上传，支持的格式："+FileEnum.image.getType());
        }

        String fileName = file.getOriginalFilename();
        int size = (int) file.getSize();
        assert fileName != null;
        String fileType = fileName.substring(fileName.lastIndexOf("."));
        log.info("上传文件名:{},size:{}", fileName, size);
        String newFileName = System.currentTimeMillis() + RandomUtil.randomString(8) + fileType;
//        String path = fileService.uploadImag(file, newFileName);
//        if (StringUtils.isBlank(path)) {
//            throw new CommonException("上传文件失败", "upload_file_exception_fail");
//        }
        String absoluteFile = projectProperty.getLastDestPath() + File.separator + newFileName;
        String filePath = projectProperty.getDestPath() + absoluteFile;
        File dest = new File(filePath);
        if (!dest.getParentFile().exists()) { //判断文件父目录是否存在
            dest.getParentFile().mkdir();
        }
        dest.setReadable(true, false);
        dest.setExecutable(true, false);
        dest.setWritable(true, false);
        try {
            file.transferTo(dest); //保存文件
            SysConfig config = configService.getConfigByName(ConfigConstants.ADMIN_API_URL);
            return successQueryOneResponse(config.getConfValue() + absoluteFile);
        } catch (IllegalStateException e) {
            log.error("上传文件出错：", e);
            throw new CommonException("文件上传出错", "upload_illegal_state");
        } catch (IOException e) {
            log.error("上传文件出错,IO异常：", e);
            throw new CommonException("文件上传出错", "upload_io_exception");
        }
    }
}
